package com.example.administrator.jiguang;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2016/11/23.
 */

public class JiGuangReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";

    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        if(nm == null){
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive: "+intent.getAction()+",extras:"+bundle.toString());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");
            //receivingNotification(context,bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的通知");
           // receivingNotification(context,bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");
            jump(context);
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }
    private void receivingNotification(Context context, Bundle bundle) {
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        // 使用notification
        // 使用广播或者通知进行内容的显示
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        //setContentTitle 显示有上面的信息内容    setColor是设置图标和标题的颜色
        builder.setContentText(message).setSmallIcon(R.mipmap.ic_tu1).setContentTitle("您有新订单,请注意查收")
                .setColor(Color.RED);
        //builder.setSmallIcon(R.mipmap.ic_tu1);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        manager.notify(1,builder.build());

    }
    //页面的跳转在这里
    private void jump(Context context) {
        Intent intent = new Intent(context,Main2Activity.class);
        context.startActivity(intent);
    }

}
